import java.util.HashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:38917
 * Date:2025/4/12
 * Time:16:15
 */
public class Solution {
    public int subarraySum(int[] nums, int k) {
        HashMap<Integer, Integer> map = new HashMap<>();
        map.put(0, 1);
        int sum = 0,ret = 0;
        for (int cur : nums) {
            sum += cur;//计算当前位置的前缀和
            ret += map.getOrDefault(sum - k, 0);//统计结果
            map.put(sum, map.getOrDefault(sum, 0) + 1);
        }
        return ret;
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        int ret = solution.subarraySum(new int[]{1,2,3,2,4,3,2,1}, 5);
        System.out.println(ret);
    }
}
